import re
import pandas as pd
import chajian
def re_listChooseStr(list_a: list, choose_begin_name: str) -> str:#"250日均线[20210904]"->250日均线
    for i in list_a:
        m = re.search('^{}'.format(choose_begin_name), i)
        if m == None:
            pass
        else:
            return i

def dftore(df:pd.DataFrame)->pd.DataFrame:
    df=df
    """
     250日均线[20211008]    code market_code              买入信号inter[20211008]  技术形态[20211008]      最新价   最新涨跌幅       股票代码  股票简称
0           1907.423  600519          17                         周线macd金叉    价升量涨||阳线||放量  1839.60   0.525  600519.SH  贵州茅台
1           1031.569  688169          17         行情收盘价上穿5日||skdj金叉||rsi金叉    价升量涨||阳线||缩量   730.86   3.385  688169.SH  石头科技
2            462.155  688536          17          mtm金叉||roc买入信号||cci买入信号    缩量||回调缩量||阴线   632.00  -0.958  688536.SH   思瑞浦
3            455.285  688185          17           bias买入信号||wr超卖||rsi底背离    放量||阴线||价跌量升   306.05  -9.664  688185.SH   康希诺
"""
    df_250=chajian.re_listChooseStr(df.columns,"250日均线")

    df_hsl=chajian.re_listChooseStr(df.columns,"换手率")

    df_money=chajian.re_listChooseStr(df.columns,"成交额")

    df_zf=chajian.re_listChooseStr(df.columns,"振幅")

    df_close=chajian.re_listChooseStr(df.columns,"行情收盘价")

    columns_ = ["code", "股票简称", df_250, df_hsl,df_money,df_zf,df_close]

    df=df[columns_].rename(columns={'%s'%(df_250):'250日',
                             '%s' % (df_hsl): 'turnover',
                             '%s' % (df_money): 'amount',
                             '%s' % (df_zf): '振幅',
                             '%s' % (df_close): 'close',
                             })
    #print(df)
    df['amount'] = round(df['amount'].astype(float)/ 100000000,2)
    df['振幅'] = round(df['振幅'].astype(float) , 2)
    df['turnover'] = round(df['turnover'].astype(float), 2)
    df['close'] = round(df['close'].astype(float), 2)
    return df
def ths_vol_df(df:pd.DataFrame)->pd.DataFrame:
    list_i = []
    for i in df.columns:
        m = re.search('^{}'.format("区间成交量"), i)
        if m == None:
            pass
        else:
            list_i.append(i)
    sorted_id = sorted(range(len(list_i)), key=lambda k: list_i[k], reverse=False)
    # print('元素索引序列：', sorted_id)
    cjl = chajian.re_listChooseStr(df.columns, "成交量")
    list_i.append(cjl)

    data = df[[list_i[sorted_id[0]], list_i[sorted_id[1]], list_i[sorted_id[2]], list_i[3], 'code', "股票代码", "股票简称"]]
    data.rename(columns={list_i[sorted_id[0]]: "120日",#因为时间["区间成交量[20210602-20211126]","区间成交量[20211028-20211126]"]较小，所以是120天
                         list_i[sorted_id[1]]: "30日",
                         list_i[sorted_id[2]]: "5日",
                         list_i[3]: "成交量", }, inplace=True)
    """            120日          30日          5日            成交量    code       股票代码   股票简称
0   166192158691  48725774059  4960530630  1573909100.00  600010  600010.SH   包钢股份
1    38075188814  19042551050  3664354455   732925730.00  002610  002610.SZ   爱康科技
2    16807051789  16807051789  3231788647   628737420.00  601868  601868.SH   中国能建"""
    data['120日'] = round(data['120日'].astype(float) / 1000000 / 120, 1)
    data['30日'] = round(data['30日'].astype(float) / 1000000 / 30, 1)
    data['5日'] = round(data['5日'].astype(float) / 1000000 / 5, 1)
    data['成交量'] = round(data['成交量'].astype(float) / 1000000 / 1, 1)
    #print(data)
    """      120日     30日     5日     成交量    code       股票代码   股票简称
        0   1384.9  1624.2  992.1  1573.9  600010  600010.SH   包钢股份
        1    317.3   634.8  732.9   732.9  002610  002610.SZ   爱康科技
    """
    return data
def ths_price_df(df:pd.DataFrame)->pd.DataFrame:
    list_int = []
    list_i = []
    for i in df.columns:
        if "日均线" in i:
            # print(i[:-13])
            list_int.append(int(i[:-13]))
            list_i.append(i)

    #print(list_int)#[50, 250, 5, 20]
    sorted_id = sorted(range(len(list_int)), key=lambda k: list_int[k], reverse=False)

    data = df[[list_i[sorted_id[0]], list_i[sorted_id[1]], list_i[sorted_id[2]], list_i[sorted_id[3]], 'code', "股票代码", "股票简称"]]
    data.rename(columns={list_i[sorted_id[0]]: "5日",
                         list_i[sorted_id[1]]: "20日",
                         list_i[sorted_id[2]]: "50日",
                         list_i[sorted_id[3]]: "250日", }, inplace=True)
    return data
def zt_df(df: pd.DataFrame) -> pd.DataFrame:
    zijin=chajian.re_listChooseStr(df.columns, "资金流向")
    liebie = chajian.re_listChooseStr(df.columns, "涨停原因类别")
    jtjb = chajian.re_listChooseStr(df.columns, "几天几板")
    sczt = chajian.re_listChooseStr(df.columns, "首次涨停时间")
    columns_ = ["code","股票简称",zijin,liebie,jtjb,"最新涨跌幅",]
    df = df[columns_].rename(columns={'%s' % (zijin): '资金流向',
                                      '%s' % (liebie): '涨停原因类别',
                                      '%s' % (jtjb): '几天几板',
                                      '%s' % (sczt): '首次涨停时间',
                                      })
    return df
    """
    0   涨停[20211012]           24 non-null     object 
 1   涨停封单额[20211012]        24 non-null     object 
 2   涨停明细数据[20211012]       24 non-null     object 
 3   几天几板[20211012]         24 non-null     object 
 4   code                   24 non-null     object 
 5   最新价                    24 non-null     object 
 6   涨停原因类别[20211012]       24 non-null     object 
 7   资金流入inner[20211012]    24 non-null     object 
 8   最新涨跌幅                  24 non-null     object 
 9   market_code            24 non-null     object 
 10  首次涨停时间[20211012]       24 non-null     object 
 11  股票代码                   24 non-null     object 
 12  资金流向L1[20211012]       24 non-null     object 
 13  涨停封单量[20211012]        24 non-null     object 
 14  a股市值(不含限售股)[20211012]  24 non-null     object 
 15  涨停封单量占流通a股比[20211012]  24 non-null     float64
 16  涨停封单量占成交量比[20211012]   24 non-null     float64
 17  股票简称                   24 non-null     object 
 18  最终涨停时间[20211012]       24 non-null     object 
 19  连续涨停天数[20211012]       24 non-null     int64  
 20  资金流出inner[20211012]    24 non-null     object 
 21  涨停类型[20211012]         24 non-null     object 
 22  涨停开板次数[20211012]       24 non-null     object 
    """